package com.axend.aerosense.login.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.base.IToolBar;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.util.FragmentUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.app.AppManager;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.WebUtil;
import com.axend.aerosense.login.R;
import com.axend.aerosense.login.databinding.LoginActivityHomeBinding;
import com.axend.aerosense.login.fragment.LoginEnLoginFragment;

/**
 * @author ChenHongWei
 * @date :2020/12/12 14:57
 */

@Route(path = RouterActivityPath.Login.PAGE_LOGIN_HOME)
public class LoginHomeActivity extends MvvmBaseActivity<LoginActivityHomeBinding, BaseViewModel>
    implements IHostLayout, IToolBar {

    private LoginEnLoginFragment loginFragment;
    private long mExitTime;
    @Autowired
    boolean jumpMainPage = true;
    private boolean exit = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenAutoAdapter.match(this, 375.0f);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .navigationBarColor(R.color.black)
                .fitsSystemWindows(true)
                .autoDarkModeEnable(true)
                .init();
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
        init();
    }

    private void init(){
        loginFragment = new LoginEnLoginFragment();
        FragmentUtil.add(this, getContentId(), loginFragment, null, false);
        viewDataBinding.toolbar.baseLayout.setOnClickListener(v -> onBackPressed());
    }

    //此方法只是关闭软键盘
    private void hintKbTwo() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&getCurrentFocus()!=null){
            if (getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_home;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() == 0){
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                ToastUtils.showShort(getString(R.string.login_press_again_exit));
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
                return;

            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                exit = true;
                AppManager.getInstance().AppExit();
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(null != this.getCurrentFocus()){
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public int getContentId() {
        return R.id.login_fragment_host;
    }

    @Override
    public void setToolBarVisibility(int visible) {
        viewDataBinding.toolbar.baseLayout.setVisibility(visible);
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = () -> {
            FragmentManager manager = getSupportFragmentManager();
            if (manager != null) {
                Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                currFrag.onResume();
            }
        };

        return result;
    }

    @Override
    public void finish() {
        if (jumpMainPage && !exit){
            ARouter.getInstance() .build(RouterActivityPath.Main.PAGE_MAIN_MAIN) .navigation();
        }
        super.finish();
    }
}
